package org.example.admincontroller;

import lombok.extern.slf4j.Slf4j;
import org.example.adminservice.AdminService;
import org.example.pojo.Result;
import org.example.pojo.admindto.AdminLoginDTO;
import org.example.pojo.entity.Admin;
import org.example.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/admin/login")
public class AdminLoginController {

    @Autowired
    private AdminService adminService;

    @PostMapping
    public Result login(@RequestBody AdminLoginDTO loginDTO) {
        Admin admin = adminService.login(loginDTO);
        if(admin == null) {
            return Result.error("管理员姓名或密码错误！");
        }else{
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", admin.getId());
            claims.put("adminname", admin.getAdminname());
            claims.put("email", admin.getEmail());
            String jwt = JwtUtil.generateJwt(claims);
            log.info("登录成功!admin:"+admin.getAdminname());
            return Result.success(jwt);
        }
    }


}
